﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AutoCadConvert
{
    class Trace:Collection
    {
        bool bHasOneRightTrace = false;
        List<Point> m_Points = new List<Point>();
        public Trace(House house) : base(house) { }
        public override void AjustLineAndPoint()
        {
            //位置矫正
            Point center = m_house.Item<Points>().m_Center;
            foreach (BspObject item in m_EntityList)
            {
                if (item is Line &&(item as Line).m_Entity is Autodesk.AutoCAD.DatabaseServices.Polyline)
                {
                    if (bHasOneRightTrace == false)
                        m_Points.Clear();
                    else
                        break;

                    Autodesk.AutoCAD.DatabaseServices.Polyline entity = (item as Line).m_Entity as Autodesk.AutoCAD.DatabaseServices.Polyline;
                    for (int i = 0; i < entity.NumberOfVertices; i++)
                    {
                        if (Autodesk.AutoCAD.DatabaseServices.SegmentType.Line == entity.GetSegmentType(i))
                        {
                            Point ptSt = new Point(entity.GetLineSegmentAt(i).StartPoint)-center;
                            Point ptEnd = new Point(entity.GetLineSegmentAt(i).EndPoint)-center;
                            if (m_Points.Count == 0)
                                m_Points.Add(ptSt);
                            else if (m_Points.Last() != ptSt)
                                m_Points.Add(ptSt);

                            if (m_Points.Last() != ptEnd)
                                m_Points.Add(ptEnd);
                        }
                        else if (Autodesk.AutoCAD.DatabaseServices.SegmentType.Line == entity.GetSegmentType(i))
                        {
                            DwgConvert.PutMessage("轨迹只支持多段线");
                            break;
                        }
                    }
                    bHasOneRightTrace = true;
                }
            }
        }
        public IEnumerable<XElement> ChildXml()
        {
            int idx = 0;
            foreach (Point pt in m_Points)
                yield return new XElement("_" + idx++, pt.ToMirror().To3DString());
        }
        public override IEnumerable<XElement> ToXml()
        {
            yield return new XElement("Points", ChildXml());
        }
    }
}
